import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { CTableProps } from '@/components/CTable'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import { getSupplierList } from '@/apis/pcf/supplier/supplierList'
import { useRoute } from 'vue-router'
import { SupplierFormStatusEnum, FeedbackTypeEnum } from '@/constants/enum'

export const useSupplierList = () => {
  const route = useRoute()
  const searchForm = reactive({
    status: (route.query?.status ?? undefined) as keyof typeof SupplierFormStatusEnum | undefined,
    supplierName: undefined,
    formType: undefined,
    dataYear: undefined,
    codeOrPartName: undefined,
    queryPartSuitCode: undefined,
    // 反馈类型 TODO:字段名待确定
    feedbackType: undefined as keyof typeof FeedbackTypeEnum | undefined
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '供应商名称或编码',
      placeholder: '供应商名称或编码',
      field: 'supplierName',
      type: 'input'
    },
    {
      name: '零部件编码或名称',
      placeholder: '零部件编码或名称',
      field: 'codeOrPartName',
      type: 'input'
    },
    {
      name: '数据年份',
      field: 'dataYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    },
    {
      name: '数据表类型',
      placeholder: '数据表类型',
      field: 'formType',
      type: 'select',
      fixedOption: 'FormTypeOptions'
    },
    {
      name: '状态',
      placeholder: '状态',
      field: 'status',
      type: 'select',
      fixedOption: 'SupplierFormStatusOptions'
    },
    {
      name: '同类编码',
      placeholder: '同类编码',
      field: 'queryPartSuitCode',
      type: 'input'
    },
    {
      name: '反馈类型',
      placeholder: '反馈类型',
      field: 'feedbackType',
      type: 'select',
      fixedOption: 'FeedbackTypeOptions'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getSupplierList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      status: searchForm?.status,
      supplierName: searchForm?.supplierName,
      formType: searchForm?.formType,
      dataYear: searchForm?.dataYear,
      codeOrPartName: searchForm?.codeOrPartName,
      queryPartSuitCode: searchForm?.queryPartSuitCode,
      feedbackType: searchForm?.feedbackType
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '编号',
      field: 'formNo',
      slot: 'codeSlot',
      width: 200,
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '数据表类型',
      field: 'type',
      width: 140,
      slot: 'formType',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '所属企业',
      field: 'enterpriseName',
      minWidth: 140,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '供应商名称/编码',
      field: 'supplierName',
      slot: 'supplierNameSlot',
      minWidth: 170,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '联系人/手机号',
      field: 'contact',
      slot: 'contact',
      width: 115,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '零部件/编码',
      field: 'dataList',
      slot: 'itemSlot',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建时间',
      field: 'createTime',
      slot: 'createTime',
      width: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '更新时间',
      field: 'updateTime',
      width: 100,
      slot: 'updateTime',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据年份',
      field: 'dataYear',
      width: 90,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '状态',
      field: 'status',
      slot: 'statusSlot',
      fixed: 'right',
      width: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 90,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  }
}
